<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\View;
use think\Cookie;
use think\Request;
use app\index\model\IndexModel;

header("Content-type: text/html; charset=utf8");
class Index extends Controller
{

	public function _initialize()
	{
		parent::_initialize();

	}

	public function index()
	{
		
		$conn=ftp_connect('db.dgpaotui.com',299,60) or die("Couldn't connect to db.dgpaotui.com");
		$login=ftp_login($conn,'file_kykxmk','q3eB5eOL');
		if($login){
			$source = fopen("D:/working/han/public/upload/waimai/test.txt","r");
			try{
				ftp_fput($conn,"target.txt",$source,FTP_ASCII);
			}catch(Exception $e){
				print $e->getMessage();
			}
		}
		
		
		
		if($res){
			echo "success";
		}else{
			echo "false";
		}
		ftp_close($conn);	
		fclose($source);
	
	$view = new View();
		$cookiee = Cookie::get('user_name');
		if(!empty($cookiee)){
			//$this->redirect('Index/user_info');
		} else {
			return $view->fetch('login');
		}
		//			if($put){
//			echo '<img src="http://www.kykxmk.com/upload/waimai/'.$destination_file.'"  class="preview1">';
//					echo 'tmp:'.$source_file.'image:'.$put;	
//			}else{
//					echo '上传出错了！';
//			}

//		$time=time();
//		$str=substr($time,2);
//		$id=32345;
//		$ids=substr($id,-1);
//		echo "ids:".$ids."<br/>";
//		echo "time:".$time."<br/>";
//		echo "dingdan:".$str;
	}

	public function login()
	{
		$view = new View();
		if (empty($_POST['user_name'])) {
			$this->error('还没登录，请先登录','Index/no_login');
		}
			$user_name = $_POST['user_name'];
			$user_password = md5($_POST['user_password']);
			$hmuser = Db::query('select * from pigcms_merchant_store_staff where username=? and password=?', [$user_name, $user_password]);
			if (!empty($hmuser)) {
				foreach ($hmuser as $vo) {
					$id = $vo['id'];
					$store_name=Db::name('merchant_store')->field('name')->where(array('store_id'=>$vo['store_id']))->find();
					$m=new IndexModel;
					$powers=$m->getPowersByUid($id);
					$view->assign('powers',$powers);
					if (!empty($_POST['remember'])) {

						Cookie::set('user_id', $id, 604800);
						Cookie::set('user_name', $vo['username'], 604800);
						Cookie::set('user_password', $vo['password'], 604800);
						Cookie::set('store_id', $vo['store_id'], 604800);
						Cookie::set('store_name', $store_name['name'], 604800);
						Cookie::set('mer_id', $vo['token'], 604800);
					} else {
						Cookie::set('user_id', $id, 21600);
						Cookie::set('user_name', $vo['username'], 21600);
						Cookie::set('user_password', $vo['password'], 21600);
						Cookie::set('store_id', $vo['store_id'], 21600);
						Cookie::set('store_name', $store_name['name'], 21600);
						Cookie::set('mer_id', $vo['token'], 21600);
					}
					
					
				}
				//修改最后一次登录时间
				$date = strtotime("now");
				$data = [
						'last_time' => $date
				];
				Db::name('merchant_store_staff')->where('id', $id)->update($data);
				/********************
				4 1、写入内容到日志文件
				6 ********************/
				$action="用户登录";
				$datalog=[
						'log_time'=>$date,
						'log_uid'=>Cookie::get('user_id'),
						'log_ip'=>$_SERVER["REMOTE_ADDR"],
						'log_url'=>$_SERVER["REQUEST_URI"],
						'log_action'=>$action
				];
				Db::name('waimai_log')->insert($datalog);
				$this->redirect('Index/user_info');

			} else {
				$view->assign('str', '用户名或密码错误');
				return $view->fetch('login');
			}


	}
	public function no_login()
	{
		$view = new View();
			return $view->fetch('login');

	}

	public function addcg($order_id)
	{
		$view = new View();
		$uid=Cookie::get("user_id");
		$m=new IndexModel;
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
		$cookiee = Cookie::get('store_id');
		if (empty($cookiee)) {
			$this->error("店铺信息错误", 'index');
			exit;
		}
		$has = array(
				'order_id' => $order_id,
				'store_id' => $cookiee
		);
		$order = Db::name('waimai_order')->where($has)->find();
		if (empty($order)) {
			$this->error('订单操作有误');
			exit;
		}
				
			//获取每个订单的菜单名称和数量
$m->setSellStatus($order_id);
				$order['menu'] = $m->getGoods($order_id);
			
			//反序列化地址
			if(strlen($order['address'])>10){
				$order['address_info'] = unserialize($order['address']);
			}else{
				$order['address_info']['name']="";
				$order['address_info']['phone']="";
				$order['address_info']['address']="";
				$order['address_info']['detail']="";
			}
			

			//反序列化优惠明细

				$discount_detail = json_decode($order['discount_detail']);
				if(!empty($discount_detail)){
					$order['discount']=$discount_detail;	
				}
			if(empty($order['ps_date'])){
				$cdate=date("Y-m-d",$order['create_time']);
				$order['ps_date']=strtotime($cdate);
			}
			if(empty($order['ps_time'])){
					$attach=$order['desc'];
					 if (strpos($attach, '9:00-10:00') !== false) {
					 	$order['time_name']="9:00-10:00";
					 	$order['time_val']=9;
					 }
					 else if (strpos($attach, '10:00-11:00') !== false) {
					 	$order['time_name']="10:00-11:00";
					 	$order['time_val']=10;
					 }
					 else if (strpos($attach, '11:00-12:00') !== false) {
					 	$order['time_name']="11:00-12:00";
					 	$order['time_val']=11;
					 }
					 else if (strpos($attach, '15:00-16:00') !== false) {
					 	$order['time_name']="15:00-16:00";
					 	$order['time_val']=15;
					 }
					 else if (strpos($attach, '16:00-17:00') !== false) {
					 	$order['time_name']="16:00-17:00";
					 	$order['time_val']=16;
					 }
					 else if (strpos($attach, '17:00-18:00') !== false) {
					 	$order['time_name']="17:00-18:00";
					 	$order['time_val']=17;
					 }
					 
			}	
	
	
//won配送员列表
//		$condition_user['mer_id'] = $this->merchant_session['mer_id'];
		$condition_user['group'] = 1;
		$condition_user['status'] = 1;
		$user_list = Db::name('deliver_user')->where($condition_user)->select();
		$view->assign('psuser_list', $user_list);
		$view->assign('order', $order);

	$psdate=strtotime(date("Y-m-d"))-21600;
	$whps=array(
        'order_status'=>3,
        'create_time'=>['>',$psdate]     
    );
    $pscount=0;
    $pscount = Db::name('waimai_order')->where($whps)->count();
     $cgcount = 0;
     $time1=strtotime(date("Y-m-d"))-21600;//昨天下午6点后
    $where=array(
        'cg_status'=>3,
        'ps_date'=>['>=',$time1]
    );
    $cg = Db::name('waimai_caigou_list')->field('cg_number')->where($where)->group('cg_number')->select();
    if(!empty($cg)){
        $cgcount = count($cg);
    }
		$view->assign('cgcount',$cgcount);
		$view->assign('pscount',$pscount);
		return $view->fetch();

	}

	public function addorder($order_id=0){
		$view = new View();
		$uid=Cookie::get("user_id");
		$m=new IndexModel;
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
				
		$store_id = Cookie::get('store_id');
		$menu=array();
		$infoname=$infophone=$infoaddress="";
		if (empty($store_id)) {
			$this->error('操作错误','login');
		}
		if($order_id==0){
			$prefix=10;
			$order_number = $prefix.str_shuffle(date("YmdHms").mt_rand(10, 99).mt_rand(10, 99));
			$view->assign('order_number',$order_number);
		}else{
			//取出该订单
			$where=array(
				'order_id'=>$order_id
			);
			$order=Db::name('waimai_order')->where($where)->find();
			$menu=getmenu($order_id);
			if(strlen($order['address'])>10){
				$order['address_info']=unserialize($order['address']);
					if(empty($order['address_info']['detail'])){
						$order['address_info']['detail']="";
					}
				$infoname=$order['address_info']['name'];
				$infophone=$order['address_info']['phone'];
				$infoaddress=$order['address_info']['address'];

			}
			$view->assign('infoname',$infoname);
			$view->assign('infophone',$infophone);
			$view->assign('infoaddress',$infoaddress);
			$view->assign('order',$order);
			$view->assign('menu',$menu);
		}
		
		
	
		return $view->fetch();
		
	}
	public function addorder_addgood(){
	$view = new View();
	$uid=Cookie::get("user_id");
		$m=new IndexModel;
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);	
	$store_id = Cookie::get('store_id');
	if (empty($store_id) && !(Request::instance()->isPost())) {
		$this->error('操作错误','login');
	}
	$status=$_POST['status'];
	$order_id = $_POST['order_id'];
	$name=$_POST['name'];
	$price=$_POST['price'];
	$num=$_POST['num'];
	$guige=$_POST['guige'];
	$goods_id = rand(2000,3000);
	$create_time=time();
	if(strlen($order_id)>10){
		$number=[
			'order_number'=>$order_id,
			'store_id'=>$store_id,
			'create_time'=>$create_time,
			'is_own'=>0,
			'username'=>"",
			'userphone'=>0,
			'order_status'=>2
		];
		Db::name('waimai_order')->insert($number);
		$order=Db::name('waimai_order')->field('order_id')->where(array('order_number'=>$order_id))->find();
		$order_id = $order['order_id'];
	}
	
		
		$data=[
			'store_id'=>$store_id,
			'order_id'=>$order_id,
			'goods_id'=>$goods_id,
			'name'=>$name,
			'price'=>$price,
			'guige'=>$guige,
			'num'=>$num,
			'create_time'=>$create_time,
			'status'=>1
		];
		Db::name('waimai_sell_log')->insert($data);
	
	$this->redirect('Index/'.$status,array('order_id'=>$order_id));
}
public function addcg_addgood(){
	$view = new View();
	$uid=Cookie::get("user_id");
		$m=new IndexModel;
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);	
	$store_id = Cookie::get('store_id');
	if (empty($store_id) && !(Request::instance()->isPost())) {
		$this->error('操作错误','login');
	}
	$status=$_POST['status'];
	$order_id = $_POST['order_id'];
	$name=$_POST['name'];
	$price=$_POST['price'];
	$num=$_POST['num'];
	$guige=$_POST['guige'];
	$goods_id = rand(0,1000);
	if(!empty($name) && !empty($price) && !empty($num)){
		$create_time=strtotime(date('Y-m-d H:i'));
		$data=[
			'store_id'=>$store_id,
			'order_id'=>$order_id,
			'goods_id'=>$goods_id,
			'name'=>$name,
			'price'=>$price,
			'guige'=>$guige,
			'num'=>$num,
			'create_time'=>$create_time
		];
		Db::name('waimai_sell_log')->insert($data);
	}
	$this->redirect('Index/'.$status,array('order_id'=>$order_id));
}

public function addorder2()        //添加到采购单
{
		$view = new View();
		$uid=Cookie::get("user_id");
			$m=new IndexModel;
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);	
		$store_id = Cookie::get('store_id');
		$order_id = $_POST['order_id'];
		
		if (empty($store_id) && !(Request::instance()->isPost())) {
				$this->error('店铺信息错误', 'login');exit;
		}
		if(!empty($_POST['order_number'])){
			$number=[
				'order_number'=>$_POST['order_number']
			];
			Db::name('waimai_order')->insert($number);
			$order_id=Db::name('waimai_order')->field('order_id')->where(array('order_number'=>$order_id))->find();
		}
	

		$ps_date= $_POST['ps_date'];
		$ps_time = $_POST['ps_time'];
		$infoname = $_POST['infoname'];
		$infophone = $_POST['infophone'];
		$infoaddress = $_POST['infoaddress'];
		$desc = $_POST['desc'];
		$store_desc = $_POST['store_desc'];
		$price = $_POST['price'];
		$discount_price = $_POST['discount_price'];
		$online_pay = $_POST['online_pay'];
	
	//修改顾客收货地址
	$address_info['name'] = $infoname;
	$address_info['phone'] = $infophone;
	$address_info['address'] = $infoaddress;
	if(empty($infoname)){
		$address_info['name']="空";
	}
	if(empty($infophone)){
		$address_info['phone']="0";
	}
	if(empty($infoaddress)){
		$address_info['address']="空";
	}
	
	$address = serialize($address_info);

		//生成今天，明天，后天的日期
		if ($ps_date == 1) {
			$date = strtotime(date("Y-m-d"));
		} elseif ($ps_date == 2) {
			$date = strtotime(date("Y-m-d", strtotime("+1 day")));
		} elseif ($ps_date == 3) {
			$date = strtotime(date("Y-m-d", strtotime("+2 day")));
		}
		$action="操作：创建新订单，方法：index/addorder2()。post参数：d=". $ps_date ." time=" . $ps_time ." infoname=" . $infoname ." infophone=" .$infophone ." infoaddress=" . $infoaddress ." desc=" . $desc ." store_desc=" .$store_desc ." discount_price=" . $discount_price;

				//更新订单
				$data2 = [
						'discount_price' => $discount_price,
						'price'=>$price,
						'online_pay'=>$online_pay,
						'desc' => $desc,
						'store_desc' => $store_desc,
						'create_time'=>$date,
						'ps_date' => $date,
						'ps_time' => $ps_time,
						'address' => $address,
						'order_status' =>2
				];
				Db::name('waimai_order')->where('order_id', $order_id)->update($data2);
				$action = $action . "操作：修改waimai_order数据库，discount_price=" . $discount_price . "desc=" . $desc ."store_desc=" . $store_desc . "ps_date=" . $date ."ps_time=" . $ps_time ."address=" .$address;

	/********************
	4 1、写入内容到日志文件
	6 ********************/
//	 $file  = 'waimai_log.txt';//要写入文件的文件名（可以是任意文件名），如果文件不存在，将会创建一个
// 	 $content ="日期:" . $date . " 用户ID:" . Cookie::get('user_id') . " IP地址:". $_SERVER["REMOTE_ADDR"] . " URL是:" .$_SERVER["REQUEST_URI"] . "\n" . $action . "\n";
//	file_put_contents($file, $content,FILE_APPEND);// 这个函数支持版本(PHP 5)
$datalog=[
		'log_time'=>$date,
	'log_uid'=>Cookie::get('user_id'),
	'log_ip'=>$_SERVER["REMOTE_ADDR"],
	'log_url'=>$_SERVER["REQUEST_URI"],
	'log_action'=>$action
];
	Db::name('waimai_log')->insert($datalog);
	
		$this->redirect('Index/neworder_list');
}

public function addcg2()        //添加到采购单
{
			
		$view = new View();
		$store_id = Cookie::get('store_id');
		$order_id = $_POST['order_id'];

		if (empty($store_id) && !(Request::instance()->isPost())) {
				$this->error('店铺信息错误', 'login');exit;
		}
	$has = array(
			'order_id' => $order_id,
			'store_id' => $store_id
	);
	$order = Db::name('waimai_order')->where($has)->find();
	if (empty($order)) {
		$this->error('订单信息错误');exit;
	}

		$ps_date= $_POST['ps_date'];
		$ps_time = $_POST['ps_time'];
		$infoname = $_POST['infoname'];
		$infophone = $_POST['infophone'];
		$infoaddress = $_POST['infoaddress'];
		$desc = $_POST['desc'];
		$store_desc = $_POST['store_desc'];
		$discount_price = $_POST['discount_price'];

	//反序列化地址

	if(strlen($order['address'])>10){
					$address_info = unserialize($order['address']);
					if(empty($address_info['detail'])){
						$address_info['detail']="";
					}
					
	}
	//修改顾客收货地址
	$address_info['name'] = $infoname;
	$address_info['phone'] = $infophone;
	$address_info['address'] = $infoaddress;
	$address = serialize($address_info);

		//生成今天，明天，后天的日期
		if ($ps_date == 0) {
			$cdate=date("Y-m-d",$order['create_time']);
			$date = strtotime($cdate);
		} 
		elseif ($ps_date == 1) {
			$date = strtotime(date("Y-m-d"));
		} elseif ($ps_date == 2) {
			$date = strtotime(date("Y-m-d", strtotime("+1 day")));
		} elseif ($ps_date == 3) {
			$date = strtotime(date("Y-m-d", strtotime("+2 day")));
		}
		$action="操作：添加到采购单，方法：index/addcg2()。post参数：d=". $ps_date ." time=" . $ps_time ." infoname=" . $infoname ." infophone=" .$infophone ." infoaddress=" . $infoaddress ." desc=" . $desc ." store_desc=" .$store_desc ." discount_price=" . $discount_price;

				//更新订单
				$data2 = [
						'discount_price' => $discount_price,
						'desc' => $desc,
						'store_desc' => $store_desc,
						'ps_date' => $date,
						'ps_time' => $ps_time,
						'address' => $address,
						'order_status' => 3
				];
				Db::name('waimai_order')->where('order_id', $order_id)->update($data2);
				$action = $action . "操作：修改waimai_order数据库，discount_price=" . $discount_price . "desc=" . $desc ."store_desc=" . $store_desc . "ps_date=" . $date ."ps_time=" . $ps_time ."address=" .$address;
//检查该时间段未打印的批号是否存在
	$nwh=array(
			'ps_date'=>$date,
			'ps_time'=>$ps_time,
			'cg_status'=>3
	);
	$number_has=Db::name('waimai_caigou_list')->field('cg_number')->where($nwh)->find();
	if(!empty($number_has)){
		$cg_number=$number_has['cg_number'];
	}else{		//随机生成批号
		$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
		$cg_number = $yCode[intval(date('Y')) - 2016] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) ;
	}

//修改菜品num数量
	$wh = array(
			'order_id' => $order_id,
			'status'=>1
	);
	$ids = Db::name('waimai_sell_log')->field('goods_id')->where($wh)->select();
	foreach ($ids as $key => $vo) {
		$name=$_POST[$vo['goods_id']."name"];
		$num = $_POST[$vo['goods_id']];
		$price = $_POST[$vo['goods_id']."price"];
		$guige=$_POST[$vo['goods_id']."guige"];
		$desc=$_POST[$vo['goods_id']."desc"];
		//更新订单数量
		$data = [
				
				'num' => $num,
				'price'=>$price,
		];
		Db::name('waimai_sell_log')->where('goods_id', $vo['goods_id'])->update($data);
		$action = $action . "修改数据库waimai_sell_log,goods_id=". $vo['goods_id']. " num=" . $num;

		//将菜品存入采购单数据库
		//检查采购单是否有该菜品

		$whhas = [
				'goods_name' => $name,
				'ps_date' => $date,
				'ps_time' => $ps_time,
				'cg_status' => 3,
		];
		$caigou = Db::name('waimai_caigou_list')->field('guige,num,desc')->where($whhas)->find();

		if (!empty($caigou)) {    //如果该菜品存在，更新数量
			$num3=(int)$caigou['num'] + (int)$num;
				if(!empty($desc)){
					if(!empty($caigou['desc']) && $caigou['desc'] !=""){
						$cgdesc= $caigou['desc'] . $num . "*". $desc.",";
					}else{
						
							$cgdesc=$num."*".$desc.",";
					}
				}else{
					$cgdesc="";
				}		
				
				if(!empty($caigou['guige']) && $caigou['guige'] !=''){
					$cgguige= $caigou['guige'] . "|" . $num . "*".$guige;
				}else{
					$cgguige= $num . "*".$guige;
				}
			$d = [
					'guige'=>$cgguige,
					'num' => $num3,
					'desc'=>$cgdesc
			];
			Db::name('waimai_caigou_list')->where($whhas)->update($d);
			$action = $action ."修改waimai_caigou_list表数量，where goods_name=".$name ."ps_date=". $date ."ps_time=". $ps_time ."num=". $num3;
		} else {                    //没有就插入一条菜品
			$cgguige= $num . "*".$guige;
			if(!empty($desc) && $desc!=''){
				$cgdesc=$num."*".$desc.",";
			}else{
				$cgdesc='';
			}
			
			$caigou_data = [
					'order_id' => $order_id,
					'store_id' => $store_id,
					'cg_number'=>$cg_number,
					'goods_id' => $vo['goods_id'],
					'goods_name' => $name,
					'goods_price' => $price,
					'guige' => $cgguige,
					'num' => $num,
					'desc' => $cgdesc,
					'ps_date' => $date,
					'ps_time' => $ps_time,
					'cg_status' => 3,
			];
			Db::name('waimai_caigou_list')->insert($caigou_data);
			$action = $action ."\n插入新数据：waimai_caigou_list表，记录：where order_id=". $order_id ."goods_name=".$name ." price=".$price ." guige=" . $guige ."ps_date=". $date ."ps_time=". $ps_time ."num=". $num;
		}

	}

	/********************
	4 1、写入内容到日志文件
	6 ********************/
//	 $file  = 'waimai_log.txt';//要写入文件的文件名（可以是任意文件名），如果文件不存在，将会创建一个
// 	 $content ="日期:" . $date . " 用户ID:" . Cookie::get('user_id') . " IP地址:". $_SERVER["REMOTE_ADDR"] . " URL是:" .$_SERVER["REQUEST_URI"] . "\n" . $action . "\n";
//	file_put_contents($file, $content,FILE_APPEND);// 这个函数支持版本(PHP 5)
$datalog=[
		'log_time'=>$date,
	'log_uid'=>Cookie::get('user_id'),
	'log_ip'=>$_SERVER["REMOTE_ADDR"],
	'log_url'=>$_SERVER["REQUEST_URI"],
	'log_action'=>$action
];
	Db::name('waimai_log')->insert($datalog);

	
		$this->redirect('Index/newcg_list');
}

    //won提交指定配送人员信息
public function select_psy2(){
		$store_id=Cookie::get("store_id");
		if(empty($store_id)){
			$this->error("店铺信息错误","Index/index");
		}
		$m=new IndexModel;
	if(Request::instance()->isPost()){
		//获取订单信息
		$order_id = $_POST['order_id'];
		$dayin = $_POST['dayin'];
		if (! $order_id) {
			$this->error("订单参数错误");exit;
		}
		$whid=array(
		'order_id'=>$order_id
		);
		$order = Db::name("Waimai_order")->where($whid)->find();
		$addr=$m->getAddress($order['address']);
		$address=$addr['address'].$addr['detail'];
		if ($order['order_status'] != 3) {
			//  D()->rollback();
			$this->error("订单信息错误");exit;
		}
		$uid = $_POST['uid'];
		if($uid=='0'){$this->error("还没有选择配送员");exit;}
		//查找SUPPLY_ID
		$condition['order_id'] = $order_id;
		$supplys=Db::name("deliver_supply")->where($condition)->find();
		if (!empty($supplys)) {
			$wherecol=array(
					'supply_id'=>$supplys['supply_id']
			);
			$col=[
				'uid'=>$uid,
				'status'=>2         //1是等待接单，2是接单
			];
			//D()->startTrans();
			Db::name('deliver_supply')->where($wherecol)->update($col);
			
		}else{
			$uwh=[
			'uid'=>$uid
			];
			$duser=Db::name("deliver_user")->where($uwh)->find();
			$sdata=[
			'order_id'=>$order_id,
			'store_id'=>$store_id,
			'from_site'=>$duser['site'],
			'from_lnt'=>$duser['lng'],
			'from_lat'=>$duser['lat'],
			'aim_site'=>$address,
			'create_time'=>time(),
			'uid'=>$uid,
			'item'=>1,
			'status'=>2,
			'name'=>$order['username'],
			'phone'=>$order['userphone']
			];
			Db::name('deliver_supply')->insert($sdata);
		}
	
		$action = "操作：配送员接单。uid=" . $uid . " status=2";
			
		

		/********************
		4 1、写入内容到日志文件
		6 ********************/
		$date=strtotime(date("Y-m-d H:i:s"));
		$datalog=[
				'log_time'=>$date,
				'log_uid'=>Cookie::get('user_id'),
				'log_ip'=>$_SERVER["REMOTE_ADDR"],
				'log_url'=>$_SERVER["REQUEST_URI"],
				'log_action'=>$action
		];
		Db::name('waimai_log')->insert($datalog);
		$this->redirect('Index/ps_list',array("dayin"=>$dayin,"target"=>$order_id));

	}else{
		$this->error("您已经进入二次元空间，请原路返回！");exit;
	}
}
public function select_psy($order_id,$dayin){		//指定派送员
		$view = new View();
		$m=new IndexModel;
		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);		
		$cookiee = Cookie::get('store_id');
		if(empty($cookiee)){
			$this->error("店铺信息错误");exit;
		}
    		//查询该订单是否存在
    			$where3 = array(
    				'store_id'=>$cookiee,
    				'order_id'=>$order_id,
    			);
    			$order = Db::name('waimai_order')->where($where3)->find();  
    		if(empty($order)){
				$this->error("订单不存在");exit;
			}	  			
            if ($order['order_status'] != 3) {
                $this->error("订单信息错误");exit;
            }
    		
		
		//查询是否已有配送员
		
		$uid = Db::name('deliver_supply')->field('uid')->where(array('order_id'=>$order_id))->find();
		if(!empty($uid)){	//表示已存在
			$duser = Db::name('deliver_user')->field('uid,name')->where(array('uid'=>$uid['uid']))->find();
			$view->assign('duser', $duser);
		}
		
		
		
    	//<--start-->   获取所有配送员今天接到的订单数量	
    		$time1 = strtotime(date("Y-m-d"))-21600;
			$time2 = $time1+86400;
			$s=array(
				'status'=>['>',1],
				'create_time'=>[['>=',$time1],['<',$time2],'and']
			);
			$scount=Db::name("deliver_supply")->field('uid,count(uid)')->where($s)->group('uid')->select();
			$user_list = Db::name('deliver_user')->field('uid,name')->select();
		
			if(!empty($scount['0'])){
				foreach($scount as $k=>$c){
					$psusers_c[$c['uid']]['count']=$c['count(uid)'];
				}
			}
			
			foreach($user_list as $k=>$c){
					$psusers_c[$c['uid']]['name']=$c['name'];
					$psusers_c[$c['uid']]['uid']=$c['uid'];
			}
		//<--end--> 	
			//获取配送员列表
//		$condition_user['mer_id'] = $this->merchant_session['mer_id'];
        $condition_user['group'] = 1;
		$condition_user['status']=1;
		$user_list = Db::name('deliver_user')->where($condition_user)->select();
		$view->assign('deliver_list', $user_list);
		$view->assign('order_id', $order_id);
		$view->assign('counts', $psusers_c);	
		$view->assign('dayin', $dayin);
			return $view->fetch();	
}
    
public function newcg_list(){
	$view = new View();
	$m=new IndexModel;
	$uid=Cookie::get("user_id");
	$powers=$m->getPowersByUid($uid);
	$view->assign('powers',$powers);
			
	$ce=Cookie::get('store_id');
	$c9=$c10=$c11=$c15=$c16=$c17=0;
			$t1=$t2=0;
	$wei=0;
	$day1=array();
	$day2=array();
	$day3=array();
	$weizhi=array();
	$date1=strtotime(date("Y-m-d"));//今天
	$date2=$date1+86400;//明天
	$date3=$date2+86400;//后天
	if(empty($ce)){
		$this->error("店铺信息错误","Index/index");exit;
	}
	
			$where=array(
				'cg_status'=>3,
				'ps_date'=>['>=',$date1]
			);
			$newcg_list = Db::name('waimai_caigou_list')->where($where)->order('ps_time')->select();
			 $pscount =$m->pscount();
    		$cgcount = $m->cgcount();
$default=0;
$dmenu=array();
for($i=9;$i<=17;$i++){
	$n=$i+1;
	if($i==12 || $i==13 || $i==14)continue;
	$dmenu[$i]['time']=$i.":00-".$n.":00";
	$dmenu[$i]['num']=0;
}
$d2=array();
$d3=array();
$cs=array();
		if(!empty($newcg_list['0'])){

			foreach($newcg_list as $key=>$vo){
				$ps_date=$vo['ps_date'];
				$t=$vo['ps_time'];

				if($ps_date == $date1){
					$day1[$t][$vo['cg_number']]['cg'][$vo['cg_id']]=$vo;
					$day1[$t][$vo['cg_number']]['ps_date']=$vo['ps_date'];
					$day1[$t][$vo['cg_number']]['cg_number']=$vo['cg_number'];
					$day1[$t][$vo['cg_number']]['ps_time']=$vo['ps_time'];
					if($t==9){
						$cs[9][$vo['cg_number']]=1;
						if($default==0){$default=9;}
					}elseif($t==10){
							$cs[10][$vo['cg_number']]=1;
						if($default==0){$default=10;}
					}elseif($t==11){
							$cs[11][$vo['cg_number']]=1;
						if($default==0){$default=11;}
					}elseif($t==15){
							$cs[15][$vo['cg_number']]=1;
						if($default==0){$default=15;}
					}elseif($t==16){
							$cs[16][$vo['cg_number']]=1;
						if($default==0){$default=16;}
					}elseif($t==17){
							$cs[17][$vo['cg_number']]=1;
						if($default==0){$default=17;}
					}
					
				}
				elseif($ps_date == $date2){
					$day2[$t][$vo['cg_number']]['cg'][$vo['cg_id']]=$vo;
					$day2[$t][$vo['cg_number']]['ps_date']=$vo['ps_date'];
					$day2[$t][$vo['cg_number']]['cg_number']=$vo['cg_number'];
					$day2[$t][$vo['cg_number']]['ps_time']=$vo['ps_time'];
					$d2[$vo['cg_number']]=1;
					
				}elseif($ps_date == $date3){
					$day3[$t][$vo['cg_number']]['cg'][$vo['cg_id']]=$vo;
					$day3[$t][$vo['cg_number']]['ps_date']=$vo['ps_date'];
					$day3[$t][$vo['cg_number']]['cg_number']=$vo['cg_number'];
					$day3[$t][$vo['cg_number']]['ps_time']=$vo['ps_time'];
					$d3[$vo['cg_number']]=1;
					
				}else{
					
					$weizhi[$t][$vo['cg_number']]['cg'][$vo['cg_id']]=$vo;
					$weizhi[$t][$vo['cg_number']]['ps_date']=$vo['ps_date'];
					$weizhi[$t][$vo['cg_number']]['cg_number']=$vo['cg_number'];
					$weizhi[$t][$vo['cg_number']]['ps_time']=$vo['ps_time'];
					$wei+=1;
				}
			}
				
		}
		foreach($cs as $key=>$c){
			$dmenu[$key]['num']=count($c);
		}
		$d2count=count($d2);
		$d3count=count($d3);
		
		$view->assign('d2count',$d2count);
		$view->assign('d3count',$d3count);
		$view->assign('dmenu',$dmenu);
		$view->assign('default',$default);
		$view->assign('day1',$day1);
		$view->assign('day2',$day2);
		$view->assign('day3',$day3);
		$view->assign('weizhi',$weizhi);
		$view->assign('c9',$c9);
		$view->assign('c10',$c10);
		$view->assign('c11',$c11);
		$view->assign('c15',$c15);
		$view->assign('c16',$c16);
		$view->assign('c17',$c17);
		$view->assign('t1',$t1);//明天数量
		$view->assign('t2',$t2);//后天数量
		$view->assign('wei',$wei);//后天数量
	
   
 
		$view->assign('cgcount',$cgcount);
		$view->assign('pscount',$pscount);
		
			return $view->fetch();
		
}

	public function cg_history(){
		$view = new View();
		$m=new IndexModel;
		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
				
		$ce=Cookie::get('store_id');
		if(!empty($ce)){
			if(!empty($_POST['cg_number'])){
				$view->assign('cg_number', $_POST['cg_number']);
				$where=array(
					'cg_number'=>$_POST['cg_number']
				);
				$list = Db::name('waimai_caigou_list')->where($where)->find();
				if(!empty($list)) {
					
						$view->assign('ps_date', $list['ps_date']);
						$view->assign('ps_time', $list['ps_time']);
						$view->assign('cg_list', $list);
				}

			}else{
				$first=strtotime(date("Y-m-d"));
				
				$where=array(
						'ps_date'=>$first,
						'cg_status'=>1
				);
				$list = Db::name('waimai_caigou_list')->where($where)->order('ps_time')->select();

				if(!empty($list['0'])){
						foreach($list as $key=>$vo) {
							$cg_list[$vo['cg_number']]['list'][$vo['cg_id']] = $vo;
							$cg_list[$vo['cg_number']]['ps_date'] = $vo['ps_date'];
							$cg_list[$vo['cg_number']]['ps_time'] = $vo['ps_time'];
							$cg_list[$vo['cg_number']]['cg_number'] = $vo['cg_number'];
						}
					$view->assign('cg_list',$cg_list);
				}

			}
			return $view->fetch();
		}else{
			$this->error('login');
		}

	}
public function update_cg($ps_date,$ps_time){
		$view = new View();
		$m=new IndexModel;
		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
				
		$store_id = Cookie::get('store_id');
		if(empty($store_id)){
			$this->error('店铺信息错误','index');exit;
		}
		$where=array(
			'store_id'=>$store_id,
			'ps_date'=>$ps_date,
			'ps_time'=>$ps_time,
			'cg_status'=>3
		);
		$cg = Db::name('waimai_caigou_list')->where($where)->select();
		$view->assign('cg',$cg);
		$view->assign('ps_date',$ps_date);
		$view->assign('ps_time',$ps_time);
		return $view->fetch();
}

public function ps_list($dayin="no",$target=0){
	$view = new View();
		$m=new IndexModel;
		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
				
		$cookiee = Cookie::get('store_id');
	if(empty($cookiee)){$this->error("店铺信息错误","Index/index");}	
    			//获取状态为3的待配送列表
    			$psdate=strtotime(date("Y-m-d"))-21600;//减去6小时
    			$psdate2=$psdate+86400;//加上24小时
    			$psd_start=strtotime(date("Y-m-d"));
    			$where3 = array(
    				'store_id'=>$cookiee,
    				'order_status'=>3,
    				'ps_date'=>$psd_start
    				/*'create_time'=>[['>=',$psdate],['<=',$psdate2],'and']*/ 
    			);
    			$list = Db::name('waimai_order')->where($where3)->order('ps_time')->select();
    			$ps_list=array();
    			foreach($list as $k=>$s){
    				$ps_list[$s['order_id']]=$s;
    			}
    			//按下单日期取出
    			$where3 = array(
    				'store_id'=>$cookiee,
    				'order_status'=>3,
    				'create_time'=>['>=',$psdate]
    			);
    			$clist = Db::name('waimai_order')->where($where3)->order('ps_time')->select();
    			foreach($clist as $k=>$s){
    				if(empty($ps_list[$s['order_id']])){
    					$ps_list[$s['order_id']]=$s;
    				}
    				
    			}
    			
		//获取每个订单的菜单名称和数量 'status'=>[['EQ',NULL],['=',1],'or']
		$dayin_no=array();
		$dayin_yes=array();
		foreach($ps_list as $k=>$psl){
			$ps_list[$k]['menu']=$m->getGoods($psl['order_id']);
			if(empty($psl['ps_time'])){
				$attach=$psl['desc'];
			 if (strpos($attach, '9:00-10:00') !== false) {
			 	$ps_list[$k]['ps_time']=9;
			 }
			 else if (strpos($attach, '10:00-11:00') !== false) {
			 	$ps_list[$k]['ps_time']=10;
			 }
			 else if (strpos($attach, '11:00-12:00') !== false) {
			 	$ps_list[$k]['ps_time']=11;
			 }
			 else if (strpos($attach, '15:00-16:00') !== false) {
			 	$ps_list[$k]['ps_time']=15;
			 }
			 else if (strpos($attach, '16:00-17:00') !== false) {
			 	$ps_list[$k]['ps_time']=16;
			 }
			 else if (strpos($attach, '17:00-18:00') !== false) {
			 	$ps_list[$k]['ps_time']=17;
			 }
					 
			}
			if(empty($psl['ps_date']))	{
				$psdate=date("Y-m-d",$psl['create_time']);
				$ps_list[$k]['ps_date']=strtotime($psdate);
				
			}
			$t=$ps_list[$k]['ps_time'];
			$d=$ps_list[$k]['ps_date'];
			//获取每个订单的配送员
			$deliver=$m->getDeliver($psl['order_id']);	
			if(!empty($deliver)){
				$ps_list[$k]['deliver_user'] = $deliver['name'];
   				$ps_list[$k]['deliver_uid'] = $deliver['uid'];
			}else{
				$ps_list[$k]['deliver_user'] = "";
   				$ps_list[$k]['deliver_uid'] = "";
			}	
			//获取地址
			$ps_list[$k]['address_info'] =$m->getAddress($psl['address']);
			//反序列化优惠明细
			$discount_detail = json_decode($psl['discount_detail']);
			if(!empty($discount_detail)){
				$ps_list[$k]['discount']=$discount_detail['0'];	
			}
			
			//区分已打印和未打印
			if(!empty($ps_list[$k]['address_info']['dayin'])){
				$dayin_yes[$t][$k]=$ps_list[$k];
			}else{
				$dayin_no[$t][$k]=$ps_list[$k];
			}
		}
		//dump($dayin_no);
		//dump($dayin_yes);
		
		if($dayin=="no"){
			$path="ps_list";
			$view->assign("dayin_no",$dayin_no);
		}elseif($dayin=="yes"){
			$path="ps_list_yes";
			$view->assign("dayin_yes",$dayin_yes);
		}else{
			$path="ps_list";
			$view->assign("dayin_no",$dayin_no);
		}
		//获取待处理采购单、配送单数量
		$cgcount=$m->cgcount();
		$pscount=$m->pscount();
		$view->assign("cgcount",$cgcount);
		$view->assign("pscount",$pscount);
		$view->assign("target",$target);
		
			return $view->fetch($path);
		
		
					
}	
//public function ps_list($dayin){
//		$view = new View();
//		$m=new IndexModel;
//		$uid=Cookie::get("user_id");
//		$powers=$m->getPowersByUid($uid);
//		$view->assign('powers',$powers);
//				
//		$cookiee = Cookie::get('store_id');
//	if(!empty($cookiee)){
//			
//  			//获取状态为3的待配送列表
//  			$psdate=strtotime(date("Y-m-d"))-21600;
//  			$psdate2=$psdate+86400;
//  			$where3 = array(
//  				'store_id'=>$cookiee,
//  				'order_status'=>3,
//  				'create_time'=>[['>=',$psdate],['<=',$psdate2],'and'] 
//  			);
//  			$ps_list = Db::name('waimai_order')->where($where3)->order('order_id')->select();
//  			
//		$c9=$c10=$c11=$c15=$c16=$c17=0;
//				$c2=$c3=0;
//				
//		$day0=array();
//		$day1=array();
//		$day2=array();
//		$day3=array();
//			
//	
//		$date1=strtotime(date("Y-m-d"));//今天
//		$date2=$date1+86400;//明天
//		$date3=$date2+86400;//后天
//		
//		if (!empty($ps_list['0'])){
//
//			foreach($ps_list as $k=>$psl){
//				$order_id = $psl['order_id'];
//				if(empty($psl['ps_time'])){
//					$attach=$psl['desc'];
//					 if (strpos($attach, '9:00-10:00') !== false) {
//					 	$ps_list[$k]['desc_time']="9:00-10:00";
//					 	$ps_list[$k]['ps_time']=9;
//					 }
//					 else if (strpos($attach, '10:00-11:00') !== false) {
//					 	$ps_list[$k]['desc_time']="10:00-11:00";
//					 	$ps_list[$k]['ps_time']=10;
//					 }
//					 else if (strpos($attach, '11:00-12:00') !== false) {
//					 	$ps_list[$k]['desc_time']="11:00-12:00";
//					 	$ps_list[$k]['ps_time']=11;
//					 }
//					 else if (strpos($attach, '15:00-16:00') !== false) {
//					 	$ps_list[$k]['desc_time']="15:00-16:00";
//					 	$ps_list[$k]['ps_time']=15;
//					 }
//					 else if (strpos($attach, '16:00-17:00') !== false) {
//					 	$ps_list[$k]['desc_time']="16:00-17:00";
//					 	$ps_list[$k]['ps_time']=16;
//					 }
//					 else if (strpos($attach, '17:00-18:00') !== false) {
//					 	$ps_list[$k]['desc_time']="17:00-18:00";
//					 	$ps_list[$k]['ps_time']=17;
//					 }
//					 
//				}
//				if(empty($psl['ps_date']))	{
//					$day0[$k]=$psl;
//				}else{
//					if($psl['ps_date']==$date1){
//						$t=$psl['ps_time'];
//						$day1[$t][$k]=$psl;
//						if($t==9){
//							$c9=1;
//						}else if($t==10){
//							$c10=1;
//						}else if($t==11){
//							$c11=1;
//						}else if($t==15){
//							$c15=1;
//						}else if($t==16){
//							$c16=1;
//						}else if($t==17){
//							$c17=1;
//						}
//						
//						
//					}
//					else if ($psl['ps_date']==$date2){
//						$day2[$k]=$psl;
//					}
//					else if ($psl['ps_date']==$date3){
//						$day3[$k]=$psl;
//					}
//				}
//								//获取每个订单的菜单名称和数量 'status'=>[['EQ',NULL],['=',1],'or']
//					$n = array(
//					    'order_id' => $order_id,
//					);
//					$menu1=array();
//					$menu1 = Db::name('waimai_sell_log')->field('goods_id,name,price,guige,num,gkid,gwid,status')->where($n)->select();
//					foreach ($menu1 as $key => $vo) {
//					        	if(empty($vo['status'])){
//					        		$whstatus=array(
//					        			'order_id'=>$order_id,
//					        			'goods_id'=>$vo['goods_id']
//					        		);
//					        		$data_status=[
//					        			'status'=>1
//					        		];
//					        		Db::name('waimai_sell_log')->where($whstatus)->update($data_status);
//					        	}
//					}
//					$n2 = array(
//					    'order_id' => $order_id,
//					    'status'=>1
//					);
//					$menu=array();
//					$menu = Db::name('waimai_sell_log')->field('goods_id,name,price,guige,num,gkid,gwid,status')->where($n)->select();
//					
//					    if (!empty($menu['0'])) {
//					       
//					        foreach ($menu as $key => $vo) {
//					        	
//								$menu[$key]['allprice'] = ($menu[$key]['price'])*($menu[$key]['num']);
//
//					        	if(empty($vo['status'])){
//					        		$whstatus=array(
//					        			'order_id'=>$order_id,
//					        			'goods_id'=>$vo['goods_id']
//					        		);
//					        		$data_status=[
//					        			'status'=>1
//					        		];
//					        		Db::name('waimai_sell_log')->where($whstatus)->update($data_status);
//					        	}
//					            
//					           if(empty($vo['name'])){
//					
//					                $goods = Db::name('waimai_goods')->field('goods_id,name,price,unit,gjson,gweight')->where('goods_id', $vo['goods_id'])->find();
//					                if (!empty($goods)) {
//					                 
//					                            $menu[$key]['name'] = $goods['name'];
//					                            $menu[$key]['price'] = $goods['price'];
//					                             
//					                            //获取一元购的菜品规格一
//					                            $gjson = json_decode($goods['gjson'], true);
//					
//					                            if (!empty($gjson[$vo['gkid'] - 1])) {
//					                                $menu[$key]['name'] = $gjson[$vo['gkid'] - 1]['0'];
//					                                $menu[$key]['price'] = $gjson[$vo['gkid'] - 1]['1'];
//					                            } else if (!empty($gjson['0'])) {
//					                                $menu[$key]['name'] = $gjson['0']['0'];
//					                                $menu[$key]['price'] = $gjson['0']['1'];
//					                            }
//					
//					                            //获取一元购的菜品规格二
//					                            $gweight = json_decode($goods['gweight'], true);
//					               // dump($gweight);
//					                            if (!empty($gweight[$vo['gwid'] - 1])) {
//					                               $cout=$gweight[$vo['gwid'] - 1]['0'];
//					                               $ke=$gweight[$vo['gwid'] - 1]['1'];
//					                                $menu[$key]['guige'] = $cout . "(".$ke.")";
//					                                if($gweight[$vo['gwid'] - 1]['1']=="两"){
//					                                	$menu[$key]['allprice']=(($menu[$key]['price'])/10)*(double)$cout;
//					                                }else{
//					                                	$menu[$key]['allprice']=($menu[$key]['price'])*(double)$cout*($menu[$key]['num']);
//					                                }
//					                            	
//					                            	$menu[$key]['allprice']=($menu[$key]['price'])*(double)($gweight[$vo['gwid'] - 1]['0'])*($menu[$key]['num']);
//					                            } else {
//					                              //  echo "not null 3:". $vo['gwid'];
//					                                $menu[$key]['guige'] = $goods['unit'];
//					                                $menu[$key]['allprice']=($menu[$key]['price'])*($menu[$key]['num']);
//					                            }
//					                    
//					                }
//					            }
//					        }
//					    }
//	
//    				$ps_list[$k]['menu']=$menu;
//    				$oid = array(
//  					'order_id'=>$psl['order_id']
//  				);
//    				$deliver_uid = Db::name('deliver_supply')->field('uid')->where($oid)->select(); 
//    					$ps_list[$k]['deliver_user'] = "";
//    					$ps_list[$k]['deliver_uid'] = 0; 				
//    				if(count($deliver_uid) > 0){
//    					$deliver=Db::name('deliver_user')->field('uid,name')->where('uid',$deliver_uid['0']['uid'])->find();
//    					$ps_list[$k]['deliver_user'] = $deliver['name'];
//    					$ps_list[$k]['deliver_uid'] = $deliver['uid'];
//    				}
//    				//反序列化地址
//    				$dayin_no=array();
//    				$dayin_yes=array();
//			if(strlen($psl['address'])>8){
//				$ps_list[$k]['address_info'] = unserialize($psl['address']);
//				if(empty($ps_list[$k]['address_info']['detail'])){
//					$ps_list[$k]['address_info']['detail']="";
//				}
//				$addr_zhen=explode("镇",$ps_list[$k]['address_info']['address']);
//				if(count($addr_zhen)>1){
//					$ps_list[$k]['address_info']['address']=$addr_zhen['1'];
//				}else{
//					$addr_shi=explode("市",$ps_list[$k]['address_info']['address']);
//					if(count($addr_shi)>1){
//						$ps_list[$k]['address_info']['address']=$addr_shi['1'];
//					}
//				}
//				if(!empty($ps_list[$k]['address_info']['dayin'])){
//					$dayin_yes[$k]=$psl;
//				}else{
//					$dayin_no[$k]=$psl;
//				}
//			}else{
//				$ps_list[$k]['address_info']['name']="";
//				$ps_list[$k]['address_info']['phone']="";
//				$ps_list[$k]['address_info']['address']="";
//				$ps_list[$k]['address_info']['detail']="";
//				$dayin_no[$k]=$psl;
//			}
//
//			//反序列化优惠明细
//				$discount_detail = json_decode($psl['discount_detail']);
//				if(!empty($discount_detail)){
//					$ps_list[$k]['discount']=$discount_detail['0'];	
//				}
//    				
//         			
//
//			}
//		}
//		foreach($ps_list as $k=>$psl){
//				$order_id = $psl['order_id'];
//				if(empty($psl['ps_date']))	{
//					$day0[$k]=$psl;
//				}else{
//					if($psl['ps_date']==$date1){
//						$t=$psl['ps_time'];
//						$day1[$t][$k]=$psl;
//						if($t==9){
//							$c9=1;
//						}else if($t==10){
//							$c10=1;
//						}else if($t==11){
//							$c11=1;
//						}else if($t==15){
//							$c15=1;
//						}else if($t==16){
//							$c16=1;
//						}else if($t==17){
//							$c17=1;
//						}
//						
//						
//					}
//					else if ($psl['ps_date']==$date2){
//						$day2[$k]=$psl;
//					}
//					else if ($psl['ps_date']==$date3){
//						$day3[$k]=$psl;
//					}
//				}
//				
//		}
//		
//		
//		$c0=count($day0);
//		$c2=count($day2);
//		$c3=count($day3);
//		
//				$view->assign('c2',$c2);
//				$view->assign('c3',$c3);
//				$view->assign('c0',$c0);
//					$view->assign('c9',$c9);
//					$view->assign('c10',$c10);
//					$view->assign('c11',$c11);
//					$view->assign('c15',$c15);
//					$view->assign('c16',$c16);
//					$view->assign('c17',$c17);
//				
//		$view->assign('day0',$day0);
//		$view->assign('day1',$day1);
//		$view->assign('day2',$day2);
//		$view->assign('day3',$day3);
//		
//		
//		$status="ps_list";
//		$view->assign('dayin_no',$dayin_no);
//		
//		$view->assign('list',$ps_list);
//		if($dayin=="no"){
//			$status="ps_list";
//		}elseif($dayin=="yes"){
//			$status="ps_list_yes";
//			$view->assign('dayin_yes',$dayin_yes);
//		}
//		
//		$time1=strtotime(date("Y-m-d"))-21600;//昨天下午6点后
//		$whps=array(
//	        'order_status'=>3,
//	        'create_time'=>['>',$time1]     
//	    );
//	    $pscount=0;
//	    $pscount = Db::name('waimai_order')->where($whps)->count();
//	     $cgcount = 0;
//	     $time1=strtotime(date("Y-m-d"))-21600;//昨天下午6点后
//	    $where=array(
//	        'cg_status'=>3,
//	        'ps_date'=>['>=',$time1]
//	    );
//	    $cg = Db::name('waimai_caigou_list')->field('cg_number')->where($where)->group('cg_number')->select();
//	    if(!empty($cg)){
//	        $cgcount = count($cg);
//	    }
//			$view->assign('cgcount',$cgcount);
//			$view->assign('pscount',$pscount);
//			
//			return $view->fetch($status);
//	}
//	else{
//	    	return $view->fetch('login');
//	}
//		
//}

public function ps_start($order_id){
	$view = new View();
		$m=new IndexModel;
		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);	
	$cookiee = Cookie::get('store_id');
	if(empty($cookiee)) {
		$this->error('店铺信息错误','index');
	}
		//获取状态为3的待配送列表
		$where = array(
				'store_id'=>$cookiee,
				'order_id'=>$order_id,
				'order_status'=>3
		);
		$data=[
			'order_status'=>5			//正在配送
		];
		Db::name('waimai_order')->where($where)->update($data);
	/********************
	4 1、写入内容到日志文件
	6 ********************/
	$date=strtotime(date("Y-m-d H:i:s"));
	$action="开始配送";
	$datalog=[
			'log_time'=>$date,
			'log_uid'=>Cookie::get('user_id'),
			'log_ip'=>$_SERVER["REMOTE_ADDR"],
			'log_url'=>$_SERVER["REQUEST_URI"],
			'log_action'=>$action
	];
	Db::name('waimai_log')->insert($datalog);
	$psdate=strtotime(date("Y-m-d"))-21600;
	$whps=array(
        'order_status'=>3,
        'create_time'=>['>',$psdate]     
    );
    $pscount=0;
    $pscount = Db::name('waimai_order')->where($whps)->count();
     $cgcount = 0;
     $time1=strtotime(date("Y-m-d"))-21600;//昨天下午6点后
    $where=array(
        'cg_status'=>3,
        'ps_date'=>['>=',$time1]
    );
    $cg = Db::name('waimai_caigou_list')->field('cg_number')->where($where)->group('cg_number')->select();
    if(!empty($cg)){
        $cgcount = count($cg);
    }
		$view->assign('cgcount',$cgcount);
		$view->assign('pscount',$pscount);
	$this->redirect('Index/ps_list');
}

	public function addcg_delgood(){
		
		$cookiee=cookie::get('store_id');
		if(empty($cookiee) && !(Request::instance()->isPost())) {
			$this->error("非法操作");exit;
		}
		$order_id=$_POST['order_id'];
		$goods_id=$_POST['goods_id'];
		$status=$_POST['status'];

		$has = array(
				'order_id'=>$order_id,
				'store_id'=>$cookiee
		);
		$is_has = Db::name('waimai_order')->where($has)->select();

		if(empty($is_has)){
			$this->error("订单信息错误");exit;
		}
		$where=array(
				'order_id'=>$order_id,
				'goods_id'=>$goods_id
		);

		$data=[
				'status'=>0			//状态设为0，表示已删除
		];
		Db::name('waimai_sell_log')->where($where)->update($data);
		
		/********************
		4 1、写入内容到日志文件
		6 ********************/
		$date=strtotime(date("Y-m-d H:i:s"));
		$action="加入采购单页面删除产品";
		$datalog=[
				'log_time'=>$date,
				'log_uid'=>Cookie::get('user_id'),
				'log_ip'=>$_SERVER["REMOTE_ADDR"],
				'log_url'=>$_SERVER["REQUEST_URI"],
				'log_action'=>$action
		];
		Db::name('waimai_log')->insert($datalog);
		
		$this->redirect('Index/'.$status,array('order_id'=>$order_id));

	}

public function delete_good(){
    	$cookiee=cookie::get('store_id');
    if(empty($cookiee) && !(Request::instance()->isPost())) {
		$this->error("非法操作");exit;
	}
    		$del_str=$_POST['str'];
			$strlist=explode(",",$del_str);
			$goods_id=$strlist['0'];
			$goods_name=$strlist['1'];
			$num=$strlist['2'];
			$order_id=$strlist['3'];
			$ps_date=$strlist['4'];
			$ps_time=$strlist['5'];

    		$status=$_POST['status'];

    		$has = array(
    				'order_id'=>$order_id,
    				'store_id'=>$cookiee
    			);
			$is_has = Db::name('waimai_order')->where($has)->select();

			if(empty($is_has)){
				$this->error("订单信息错误");exit;
			}
			$where=array(
				'order_id'=>$order_id,
				'goods_id'=>$goods_id
			);

			$data=[
				'status'=>0			//状态设为0，表示已删除
			];
			Db::name('waimai_sell_log')->where($where)->update($data);
	//修改采购单数量
	$flag=$m->cgupdate($order_id,$goods_id);

	/********************
	4 1、写入内容到日志文件
	6 ********************/
	$date=strtotime(date("Y-m-d H:i:s"));
	$action="删除产品";
	$datalog=[
			'log_time'=>$date,
			'log_uid'=>Cookie::get('user_id'),
			'log_ip'=>$_SERVER["REMOTE_ADDR"],
			'log_url'=>$_SERVER["REQUEST_URI"],
			'log_action'=>$action
	];
	Db::name('waimai_log')->insert($datalog);
			$this->redirect('Index/update_order',array('order_id'=>$order_id));

    	
    	
}
public function updateorder_delgood($order_id,$goods_id){
    	$cookiee=cookie::get('store_id');
    	$m=new IndexModel;
    if(empty($cookiee)) {
		$this->error("店铺信息错误");exit;
	}
			


    		$has = array(
    				'order_id'=>$order_id,
    				'store_id'=>$cookiee
    			);
			$order = Db::name('waimai_order')->where($has)->find();

			if(empty($order)){
				$this->error("订单信息错误");exit;
			}
		
			
	//修改采购单数量
	//echo $order_id."goods_id".$goods_id;
	$flag=$m->cgupdate($order_id,$goods_id);
	//echo $flag;
		$where=array(
				'order_id'=>$order_id,
				'goods_id'=>$goods_id
			);

			$data=[
				'status'=>0			//状态设为0，表示已删除
			];
			$f=Db::name('waimai_sell_log')->where($where)->update($data);


	/********************
	4 1、写入内容到日志文件
	6 ********************/
	$date=strtotime(date("Y-m-d H:i:s"));
	$action="删除产品";
	$datalog=[
			'log_time'=>$date,
			'log_uid'=>Cookie::get('user_id'),
			'log_ip'=>$_SERVER["REMOTE_ADDR"],
			'log_url'=>$_SERVER["REQUEST_URI"],
			'log_action'=>$action
	];
	Db::name('waimai_log')->insert($datalog);
		$this->redirect('Index/update_order',array('order_id'=>$order_id));

    	
    	
}
public function cancel_order($order_id){
	$cookiee=cookie::get('store_id');
    $m=new IndexModel;	
     if(!empty($cookiee)){
     	
    		$has = array(
    				'order_id'=>$order_id,	
    			);
			$is_has = Db::name('waimai_order')->where($has)->find();
			if(empty($is_has)){
				$this->error("订单信息错误");exit;
			}
			$where=array(
				'order_id'=>$order_id
			);
			$data=[
				'order_status'=>7			//状态设为7，表示已取消
			];
			Db::name('waimai_order')->where($where)->update($data);
			
		 /********************
		 4 1、写入内容到日志文件
		 6 ********************/
		 $date=strtotime("now");
		 $action="取消订单";
		 $datalog=[
				 'log_time'=>$date,
				 'log_uid'=>Cookie::get('user_id'),
				 'log_ip'=>$_SERVER["REMOTE_ADDR"],
				 'log_url'=>$_SERVER["REQUEST_URI"],
				 'log_action'=>$action
		 ];
		 Db::name('waimai_log')->insert($datalog);
		 if(!empty($_POST['status'])){
		 	$m->cgdelete($order_id);
			 $this->redirect('Index/allorder_list');
			 
		 }else{
			 $this->redirect('Index/neworder_list');
		 }

    }else{
		$this->error("订单信息错误");exit;
	}
}   	
	
public function update_order($order_id,$dayin){
		$view = new View();
		$m=new IndexModel;
		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
				
		$cookiee = Cookie::get('store_id');
		if(empty($cookiee)){
			$this->error("店铺信息错误",'index');exit;
		}
			$has = array(
    				'order_id'=>$order_id,
    				'store_id'=>$cookiee

    			);
			$order = Db::name('waimai_order')->where($has)->find();
			if(empty($order)){
				$this->error('订单操作有误','index');exit;
			}
    			
  				$order_id=$order['order_id'];
				//获取每个订单的菜单名称和数量
				$order['menu'] = $m->getGoods($order_id);
			
			//反序列化地址
			if(!empty($order['address'])){
				$order['address_info'] = unserialize($order['address']);
			}else{
				$order['address_info']['name']="";
				$order['address_info']['phone']="";
				$order['address_info']['address']="";
			}
		
           			
           		//反序列化优惠明细
           			$order['discount_detail']="";
           			$darray = json_decode($order['discount_detail']);
           			if(!empty($darray)){
           				$order['discount_detail']=$darray['0'];
           			}	
    
			$view->assign('dayin',$dayin);
    		$view->assign('order',$order);
    		return $view->fetch();
    	
			
}

public function update_order2(){
		$view = new View();
		$m=new IndexModel;
		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);		
		$cookiee = Cookie::get('store_id');
		$order_id=$_POST['order_id'];

		if(!empty($cookiee) && Request::instance()->isPost()){
			$has = array(
					'order_id'=>$order_id,
					'store_id'=>$cookiee
			);
			$order = Db::name('waimai_order')->where($has)->select();
			if(empty($order)){
				$this->error('订单信息错误','login');exit;
			}


			$d=$_POST['date'];
			$time=$_POST['time'];
			$infoname=$_POST['infoname'];
			$infophone=$_POST['infophone'];
			$infoaddress=$_POST['infoaddress'];
			$desc=$_POST['desc'];
			$store_desc=$_POST['store_desc'];
			$discount_price=$_POST['discount_price'];


			//生成今天，明天，后天的日期
			if($d == 1){
				$date=strtotime(date("Y-m-d"));
			}elseif($d == 2){
				$date= strtotime(date("Y-m-d",strtotime("+1 day")));
			}elseif($d == 3){
				$date=strtotime(date("Y-m-d",strtotime("+2 day")));
			}else{
				$date=$d;
			}
			foreach($order as $k=>$o){
				$id=$o['order_id'];
				//修改菜品num数量
				$wh = array(
						'order_id'=>$order_id,
						'status'=>1
				);
				$nums = Db::name('waimai_sell_log')->field('goods_id,num')->where($wh)->select();
				if(empty($nums['0'])){
					$this->redirect('Index/ps_list');
				}
				
				foreach($nums as $key=>$vo){
					$num = $_POST[$vo['goods_id']];
					$sellwh = array(
							'order_id'=>$order_id,
							'goods_id'=>$vo['goods_id']
					);
					$data=[
							'num'=>$num
					];
					Db::name('waimai_sell_log')->where($sellwh)->update($data);
					$action="操作：更改waimai_sell_log表订单菜品数量。order_id=". $order_id ."goods_id=" . $vo['goods_id'] ."num= ". $num;
					//修改采购单数量

					$wh2 = array(
							'goods_id'=>$vo['goods_id'],
							'ps_time'=>$_POST['ps_time'],
							'cg_status'=>3
					);
					$nums2 = Db::name('waimai_caigou_list')->field('num')->where($wh2)->find();
					$ynum=$_POST[$vo['goods_id']+20];
					$flag=0;
					if($ynum > $num){  //数量减少
						$jnum=$ynum-$num;
						$xnum=$nums2['num']-$jnum;
					}else if($ynum < $num){	//数量增多
						$jnum=$num-$ynum;
						$xnum=$nums2['num']+$jnum;
					}else if($ynum == $num){
						$flag=1;
					}
					if($flag == 0){
						if($xnum == 0){
							$cgdata=[
									'cg_status'=>0
							];
							Db::name('waimai_caigou_list')->where($wh2)->update($cgdata);
							$action = $action . "\n操作：删除采购单菜品";
						}else{
							$data2=[
									'num'=>$xnum
							];
							Db::name('waimai_caigou_list')->where($wh2)->update($data2);
							$action = $action . "\n操作：修改采购单菜品";
						}

					}

				}

				//反序列化地址
				if(!empty($o['address'])){
				$address_info = unserialize($o['address']);
				}
				//修改顾客收货地址
				$address_info['name']=$infoname;
				$address_info['phone']=$infophone;
				$address_info['address']=$infoaddress;
				$address = serialize($address_info);

			}

			$data2=[
					'discount_price'=>$discount_price,
					'desc'=>$desc,
					'store_desc'=>$store_desc,
					'ps_date'=>$date,
					'ps_time'=>$time,
					'address'=>$address
			];
			Db::name('waimai_order')->where('order_id',$order_id)->update($data2);
			$action = $action . "\n操作：修改订单表。应收：". $discount_price . "顾客备注：". $desc . "商家备注：". $store_desc . "配送日期：". $date . "配送时间：". $time;
			/********************
			4 1、写入内容到日志文件
			6 ********************/
			$date=strtotime(date("Y-m-d H:i:s"));
			$datalog=[
					'log_time'=>$date,
					'log_uid'=>Cookie::get('user_id'),
					'log_ip'=>$_SERVER["REMOTE_ADDR"],
					'log_url'=>$_SERVER["REQUEST_URI"],
					'log_action'=>$action
			];
			Db::name('waimai_log')->insert($datalog);
			$this->redirect('Index/ps_list',array("dayin"=>$_POST['dayin'],"target"=>$order_id));
		}
	}
public function option(){				//获取二级联菜品
		$m=new IndexModel;
		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
    	$bigid = $_GET["bigname"];

		
		    $where=array(
							'gcat_id'=>$bigid,
							'status'=>1	
						);
						$goods = Db::name('waimai_goods')->field('goods_id,gcat_id,name,gjson,gweight')->where($where)->select();
						 
		   foreach($goods as $k=>$v){ 
		        $select[] = $v; 
		    } 
		 
		    return json_encode($select); 
	}
	public function printcg_success(){				//采购单打印成功
	
		$cg_number = $_POST["cg_number"];
		$where=array(
				'cg_number'=>$cg_number,
		);
		$data=[
			'cg_status'=>1
		];
		Db::name('waimai_caigou_list')->where($where)->update($data);
		$this->redirect('Index/newcg_list');
	}
public function printcg($cg_number=0){
		$view = new View();
		$m=new IndexModel;
		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
				
		$ce=Cookie::get('store_id');
		if(empty($ce)){
		$this->error("店铺信息错误",'Index/login');exit;	
		}
				$where=array(
					'cg_number'=>$cg_number
				);
				$list = Db::name('waimai_caigou_list')->where($where)->select();
				if(!empty($list['0'])) {
						$view->assign('ps_date', $list['0']['ps_date']);
						$view->assign('ps_time', $list['0']['ps_time']);
						$view->assign('cg_number', $cg_number);
						$view->assign('cg_list', $list);
				}	
			return $view->fetch();
	}
public function printps2($order_id){
	$m=new IndexModel;
	$where=array(
					'order_id'=>$order_id
				);
	$order= Db::name('waimai_order')->where($where)->find();
	$addr=$m->getAddress($order['address']);
	$addr['dayin']="yes";
	$address=serialize($addr);
	$data=[
	'address'=>$address
	];
	Db::name('waimai_order')->where($where)->update($data);
	$this->redirect("Index/ps_list",array("dayin"=>"yes"));
}
public function printps($order_id=0){
		$view = new View();
		$m=new IndexModel;
		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
				
		$ce=Cookie::get('store_id');
		if(empty($ce)){
		$this->error("店铺信息错误",'Index/login');exit;	
		}
		
		$where=array(
					'order_id'=>$order_id
				);
		$list = Db::name('waimai_order')->where($where)->find();
			
					
			//获取每个订单的菜单名称和数量

			$list['menu']=$m->getGoods($order_id);
			
			
      				$oid = array(
    					'order_id'=>$order_id
    				);
      				$deliver_uid = Db::name('deliver_supply')->field('uid')->where($oid)->find(); 
      				if(!empty($deliver_uid)){
      					$deliver=Db::name('deliver_user')->field('uid,name')->where('uid',$deliver_uid['uid'])->find();
      					$list['deliver_user'] = $deliver['name'];
      					$list['deliver_uid'] = $deliver['uid'];
      				}
      				
      				
      					//反序列化地址
			if(!empty($list['address'])){
				$list['address_info'] = unserialize($list['address']);
				
				if(empty($list['address_info']['detail'])){
					$list['address_info']['detail']="";
				}
				$addr_zhen=explode("虎门镇",$list['address_info']['address']);
				if(count($addr_zhen)>1){
					$list['address_info']['address']=$addr_zhen['1'];
				}else{
					$addr_shi=explode("东莞市",$list['address_info']['address']);
					if(count($addr_shi)>1){
						$list['address_info']['address']=$addr_shi['1'];
					}
				}
			}else{
				$list['address_info']['name']="";
				$list['address_info']['phone']="";
				$list['address_info']['address']="";
			}
			

			
           			//反序列化优惠明细
           			$list['discount_detail']="";
 					if(!empty($list['address'])){
 						$darray = json_decode($list['discount_detail']);
 					}
           			
           			if(!empty($darray)){
           				$list['discount_detail']=$darray['0'];
           			}
			if(empty($list['ps_time'])){
					$attach=$list['desc'];
					 if (strpos($attach, '9:00-10:00') !== false) {
					 	$list['desc_time']="9:00-10:00";
					 }
					 else if (strpos($attach, '10:00-11:00') !== false) {
					 	$list['desc_time']="10:00-11:00";
					 }
					 else if (strpos($attach, '11:00-12:00') !== false) {
					 	$list['desc_time']="11:00-12:00";
					 }
					 else if (strpos($attach, '15:00-16:00') !== false) {
					 	$list['desc_time']="15:00-16:00";
					 }
					 else if (strpos($attach, '16:00-17:00') !== false) {
					 	$list['desc_time']="16:00-17:00";
					 }
					 else if (strpos($attach, '17:00-18:00') !== false) {
					 	$list['desc_time']="17:00-18:00";
					 }
					 
				}
			
			
			$view->assign('order', $list);
			return $view->fetch();
	}


	function neworder_list(){		//新订单列表
  
    	 $view = new View();
 		$m=new IndexModel;
 		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);

		$view->assign('powers',$powers);
		   	 
    	 $cookiee = Cookie::get('store_id');
    	if(!empty($cookiee)){
    			//获取状态为2的待处理列表
    			$daybefore=strtotime(date("Y-m-d"))-21600;
    			$whorder=array(
    				'store_id'=>$cookiee,
    				'order_status'=>2,
    				'create_time'=>['>=',$daybefore]
    			);
  					$newlist = Db::name('waimai_order')->where($whorder)->select();
					
				//获取每个订单的菜单名称和数量
				foreach($newlist as $k=>$list){
					$order_id=$list['order_id'];
					$ps_time=$list['ps_time'];
					if(empty($ps_time)){
						$attach=$list['desc'];
						 if (strpos($attach, '9:00-10:00') !== false) {
						 	$newlist[$k]['ps_time']=9;
						 	$newlist[$k]['time']='9:00-10:00';
						 }
						 else if (strpos($attach, '10:00-11:00') !== false) {
						 	$newlist[$k]['ps_time']=10;
						 	$newlist[$k]['time']='10:00-11:00';
						 }
						 else if (strpos($attach, '11:00-12:00') !== false) {
						 	$newlist[$k]['ps_time']=11;
						 	$newlist[$k]['time']='11:00-12:00';
						 }
						 else if (strpos($attach, '15:00-16:00') !== false) {
						 	$newlist[$k]['ps_time']=15;
						 	$newlist[$k]['time']='15:00-16:00';
						 }
						 else if (strpos($attach, '16:00-17:00') !== false) {
						 	$newlist[$k]['ps_time']=16;
						 	$newlist[$k]['time']='16:00-17:00';
						 }
						 else if (strpos($attach, '17:00-18:00') !== false) {
						 	$newlist[$k]['ps_time']=17;
						 	$newlist[$k]['time']='17:00-18:00';
						 }
					 
					}else{
						if($ps_time == 9){
							$newlist[$k]['time']='9:00-10:00';
						}elseif($ps_time == 10){
							$newlist[$k]['time']='10:00-11:00';
						}elseif($ps_time == 11){
							$newlist[$k]['time']='11:00-12:00';
						}elseif($ps_time == 15){
							$newlist[$k]['time']='15:00-16:00';
						}elseif($ps_time == 16){
							$newlist[$k]['time']='16:00-17:00';
						}elseif($ps_time == 17){
							$newlist[$k]['time']='17:00-18:00';
						}
												
					}
					if(empty($list['ps_date']))	{
						$psdate=date("Y-m-d",$list['create_time']);
						$newlist[$k]['ps_date']=strtotime($psdate);
						
					}
	      		//获取每个订单的菜单名称和数量 'status'=>[['EQ',NULL],['=',1],'or']
					
      				$newlist[$k]['menu']=$m->getGoods($order_id);
	      			//反序列化地址
	      			
					if(strlen($list['address'])>10){
						$newlist[$k]['address_info'] = unserialize($list['address']);
						if(empty($newlist[$k]['address_info']['detail'])){
							$newlist[$k]['address_info']['detail']="";
						}
						
						$addr_zhen=explode("虎门镇",$newlist[$k]['address_info']['address']);
						if(count($addr_zhen)>1){
							$newlist[$k]['address_info']['address']=$addr_zhen['1'];
						}else{
							$addr_shi=explode("东莞市",$newlist[$k]['address_info']['address']);
							if(count($addr_shi)>1){
								$newlist[$k]['address_info']['address']=$addr_shi['1'];
							}
						}
					}else{
						$newlist[$k]['address_info']['name']="";
						$newlist[$k]['address_info']['phone']="";
						$newlist[$k]['address_info']['address']="";
						$newlist[$k]['address_info']['detail']="";
					}

           	}
					$view->assign('list',$newlist);
	
				$cgcount=$m->cgcount();
				$pscount=$m->pscount();
					$view->assign('cgcount',$cgcount);
					$view->assign('pscount',$pscount);
					
					
	    	return $view->fetch('neworder_list');
	    }else{
	    	return $view->fetch('login');
	    }
}
function update_pass(){
	 $view = new View();
	 $uid=Cookie::get("user_id");
		$m=new IndexModel;
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
			 
    	 $user_id = Cookie::get('user_id');
    	 $store_id = Cookie::get('store_id');
    	if(empty($user_id)){
    		$this->error('用户信息错误','index');exit;
    	}

		return $view->fetch();
}	
function update_pass2(){
	 $view = new View();
		$m=new IndexModel;
		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
			 
    	 $user_id = Cookie::get('user_id');
    	 $store_id = Cookie::get('store_id');
    	 $pass=Cookie::get('user_password');
    	 
    	if(!empty($user_id) && !(Request::instance()->isPost())){
    		$this->error('用户信息错误','index');exit;
    	}
    	$pass1=md5($_POST['pass']);
    	$pass2=$_POST['pass2'];
    	$pass3=$_POST['pass3'];

    	if($pass1 != $pass){
			$this->error('原密码错误');exit;
		}
		if($pass2 != $pass3){
			$this->error('新密码输入不一致');exit;
		}
		
		$where=array(
    		'id'=>$user_id
    	);
		$newpass=md5($pass3);
    	$data=[
    		'password'=>$newpass
    	];
    	Db::name('merchant_store_staff')->where($where)->update($data);
	/********************
	4 1、写入内容到日志文件
	6 ********************/
	$date=strtotime(date("Y-m-d H:i:s"));
	$datalog=[
			'log_time'=>$date,
			'log_uid'=>Cookie::get('user_id'),
			'log_ip'=>$_SERVER["REMOTE_ADDR"],
			'log_url'=>$_SERVER["REQUEST_URI"],
			'log_action'=>"用户修改密码"
	];
	Db::name('waimai_log')->insert($datalog);

		Cookie::set('user_password',$newpass);
		$this->success('修改成功','index');
}	

	
function user_info(){
	 $view = new View();
	 $uid=Cookie::get("user_id");
		$m=new IndexModel;
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
			 
    	 $store_id = Cookie::get('store_id');
    	if(empty($uid)){
    		$this->error('用户信息错误','index');exit;
    	}
    	$where=array(
    		'id'=>$uid,
    		'store_id'=>$store_id
    	);
    	$staff=Db::name('merchant_store_staff')->where($where)->find();
    	$store=Db::name('merchant_store')->where(array('store_id'=>$store_id))->find();
    	if(!empty($staff)){
    		$view->assign('staff',$staff);
    	}
		
		$view->assign('store',$store);
		return $view->fetch();
}	
function update_userinfo2(){
	 $view = new View();
		$m=new IndexModel;
		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
			 
    	 $user_id = Cookie::get('user_id');
    	if(!empty($user_id) && !(Request::instance()->isPost())) {
			$this->error('用户信息错误', 'index');exit;
		}
    	$where=array(
    		'id'=>$_POST['id']
    	);
    	$data=[
    		'name'=>$_POST['name'],
    		'tel'=>$_POST['tel']
    	];
    	Db::name('merchant_store_staff')->where($where)->update($data);
	/********************
	4 1、写入内容到日志文件
	6 ********************/
	$date=strtotime(date("Y-m-d H:i:s"));
	$action="修改用户个人信息";
	$datalog=[
			'log_time'=>$date,
			'log_uid'=>Cookie::get('user_id'),
			'log_ip'=>$_SERVER["REMOTE_ADDR"],
			'log_url'=>$_SERVER["REQUEST_URI"],
			'log_action'=>$action
	];
	Db::name('waimai_log')->insert($datalog);
    	$this->redirect('Index/user_info');
}		
	 
public function order($id)			//获取一个订单详细
    {
		$view = new View();
		$m=new IndexModel;
		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
				
		$cookiee = Cookie::get('store_id');
		if(empty($cookiee)) {
			$this->error('店铺信息错误','index');
		}
		$order_id=$id;
		//根据订单号查询
		$wh = array(
				'order_id'=>$order_id
		);
		$order = Db::name("waimai_order")->where($wh)->find();
	
					$newname = Db::name('user')->field('uid,nickname,add_time')->where('uid',$order['uid'])->find();
					$order['nickname']=$newname['nickname'];
					$order['add_time']=$newname['add_time'];
				
						//获取每个订单的菜单名称和数量 'status'=>[['EQ',NULL],['=',1],'or']
					
			$order['menu']=$m->getGoods($order_id);
$sell=$m->getSellLog($order_id);
$view->assign("sell",$sell);
      				$deliver_uid = Db::name('deliver_supply')->field('uid')->where($wh)->select(); 
      					$order['deliver_user'] = "";
      					$order['deliver_uid'] = 0; 				
      				if(count($deliver_uid) > 0){
      					$deliver=Db::name('deliver_user')->field('uid,name')->where('uid',$deliver_uid['0']['uid'])->find();
      					$order['deliver_user'] = $deliver['name'];
      					$order['deliver_uid'] = $deliver['uid'];
      				}
			//反序列化地址
			if(!empty($order['address'])){
				$order['address_info'] = unserialize($order['address']);
				if(empty($order['address_info']['detail'])){
					$order['address_info']['detail']="";
				}
				
				$addr_zhen=explode("虎门镇",$order['address_info']['address']);
				if(count($addr_zhen)>1){
					$order['address_info']['address']=$addr_zhen['1'];
				}else{
					$addr_shi=explode("东莞市",$order['address_info']['address']);
					if(count($addr_shi)>1){
						$order['address_info']['address']=$addr_shi['1'];
					}
				}
			}else{
				$order['address_info']['name']="";
				$order['address_info']['phone']="";
				$order['address_info']['address']="";
				$order['address_info']['detail']="";
			}
			
			//反序列化优惠明细
			$order['discount_detail']="";
			$darray = json_decode($order['discount_detail']);
			if(!empty($darray)){
				$order['discount_detail']=$darray['0'];
			}
		
		
		$view->assign('order',$order);
		return $view->fetch();

	}

	public function search_order()			//搜索订单号
	{
		$view = new View();
		$m=new IndexModel;
		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
				
		$cookiee = Cookie::get('store_id');
		if(empty($cookiee)) {
			$this->error('店铺信息错误','index');
		}
		$order_id=$_POST['order_id'];
		//根据订单号查询
		$wh = array(
				'order_id'=>$order_id
		);
		$order = Db::name("waimai_order")->where($wh)->find();
					$newname = Db::name('user')->field('uid,nickname,add_time')->where('uid',$order['uid'])->find();
					$order['nickname']=$newname['nickname'];
					$order['add_time']=$newname['add_time'];
					
		if(empty($order)) {
			$this->error('订单不存在');exit;
		}
			//获取每个订单的菜单名称和数量
				$order['menu']=$m->getGoods($order_id);

				//反序列化地址
				if(!empty($order['address'])){
					$order['address_info'] = unserialize($order['address']);
					if(empty($order['address_info']['detail'])){
						$order['address_info']['detail']="";
					}
					$addr_zhen=explode("虎门镇",$order['address_info']['address']);
					if(count($addr_zhen)>1){
						$order['address_info']['address']=$addr_zhen['1'];
					}else{
						$addr_shi=explode("东莞市",$order['address_info']['address']);
						if(count($addr_shi)>1){
							$order['address_info']['address']=$addr_shi['1'];
						}
					}
				}
				//反序列化优惠明细
				$order['discount_detail']="";
				$darray = json_decode($order['discount_detail']);
				if(!empty($darray)){
					$order['discount_detail']=$darray['0'];
				}
				$deliver_uid = Db::name('deliver_supply')->field('uid')->where($wh)->select(); 
      					$order['deliver_user'] = "";
      					$order['deliver_uid'] = 0; 				
      				if(count($deliver_uid) > 0){
      					$deliver=Db::name('deliver_user')->field('uid,name')->where('uid',$deliver_uid['0']['uid'])->find();
      					$order['deliver_user'] = $deliver['name'];
      					$order['deliver_uid'] = $deliver['uid'];
      				}
		$view->assign('order',$order);
		return $view->fetch('order');

	}
public function allorder_list()		//所有订单
    {
		$view = new View();
		$m=new IndexModel;
		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);			 
			 $cookiee = Cookie::get('store_id');
			if(empty($cookiee)) {
					$this->error('店铺信息错误','login');
			}
    			//获取今天所有订dan
			$todate = strtotime(date("Y-m-d"));
			$list=array();
		if(empty($_POST['status'])){
				//如果参数为空，就查询今天所有订单
				$time1=strtotime(date("Y-m-d"));
				$wh = array(
						'ps_date'=>$time1,
				);
				$order_list= Db::name("waimai_order")->where($wh)->order('ps_time')->select();
				
				foreach($order_list as $key=>$vo){
					$list[$vo['order_id']]=$vo;
				}
				//按下单日期取出
				$c1=$time1-21600;
				$c2=$c1+86400;
				$whc = array(
						'create_time'=>[['>=',$c1],['<',$c2],'and'],
						//'order_status'=>[['=',1],['=',3],['=',4],['=',5],['=',8],['=',9],'or']
				);
				$clist = Db::name("waimai_order")->where($whc)->order('ps_time')->select();
				foreach($clist as $key=>$vo){
					if(empty($list[$vo['order_id']])){
						$list[$vo['order_id']]=$vo;
					}	
				}
				

		}else{
				$status=$_POST['status'];
				if($status == "day") {
					//按日期查询
					$time1 = strtotime($_POST['ps_date']);
					$wh = array(
						'ps_date'=>$time1,
						//'order_status'=>[['=',1],['=',3],['=',4],['=',5],['=',8],['=',9],'or']
					);
					$order_list= Db::name("waimai_order")->where($wh)->order('ps_time')->select();

					
					foreach($order_list as $key=>$vo){
						$list[$vo['order_id']]=$vo;
					}
					//按下单日期取出
					$c1=$time1-21600;
					$c2=$c1+86400;
					$whc = array(
							'create_time'=>[['>=',$c1],['<',$c2],'and'],
							//'order_status'=>[['=',1],['=',3],['=',4],['=',5],['=',8],['=',9],'or']
					);
					$clist = Db::name("waimai_order")->where($whc)->order('ps_time')->select();
					foreach($clist as $key=>$vo){
						if(empty($list[$vo['order_id']])){
							$list[$vo['order_id']]=$vo;
						}	
					}
					
				
				} elseif($status == "month") {
					//按月份查询
					$marray=explode("/",$_POST['ps_date']);
					$year=$marray['0'];
					$month=$marray['1'];
					
					$t=$year . "-".$month."-01";
					$days =date('t', strtotime($t));  //计算一个月有多少天
					 
					$first=$_POST['ps_date'] . "/01";
					$last=$_POST['ps_date'] . "/" . $days;
					$time1=strtotime($first)-21600;
					$time2=strtotime($last)+64800;
					$wh = array(
							'create_time'=>[['<',$time2],['>',$time1],'and'],
//							'order_status'=>[['=',0],['=',1],['=',3],['=',4],['=',5],['=',8],['=',9],'or']
					);
					$list = Db::name('waimai_order')->where($wh)->order('create_time desc')->select();
				
				}
				
			}
			foreach($list as $k=>$vo){
					$ps_time=$vo['ps_time'];
					if(empty($ps_time)){
						$attach=$vo['desc'];
						 if (strpos($attach, '9:00-10:00') !== false) {
						 	$list[$k]['ps_time']=9;
						 	$list[$k]['time']='9:00-10:00';
						 }
						 else if (strpos($attach, '10:00-11:00') !== false) {
						 	$list[$k]['ps_time']=10;
						 	$list[$k]['time']='10:00-11:00';
						 }
						 else if (strpos($attach, '11:00-12:00') !== false) {
						 	$list[$k]['ps_time']=11;
						 	$list[$k]['time']='11:00-12:00';
						 }
						 else if (strpos($attach, '15:00-16:00') !== false) {
						 	$list[$k]['ps_time']=15;
						 	$list[$k]['time']='15:00-16:00';
						 }
						 else if (strpos($attach, '16:00-17:00') !== false) {
						 	$list[$k]['ps_time']=16;
						 	$list[$k]['time']='16:00-17:00';
						 }
						 else if (strpos($attach, '17:00-18:00') !== false) {
						 	$list[$k]['ps_time']=17;
						 	$list[$k]['time']='17:00-18:00';
						 }
					 
					}else{
						if($ps_time == 9){
							$list[$k]['time']='9:00-10:00';
						}elseif($ps_time == 10){
							$list[$k]['time']='10:00-11:00';
						}elseif($ps_time == 11){
							$list[$k]['time']='11:00-12:00';
						}elseif($ps_time == 15){
							$list[$k]['time']='15:00-16:00';
						}elseif($ps_time == 16){
							$list[$k]['time']='16:00-17:00';
						}elseif($ps_time == 17){
							$list[$k]['time']='17:00-18:00';
						}
												
					}
					if(empty($list['ps_date']))	{
						$psdate=date("Y-m-d",$vo['create_time']);
						$list[$k]['ps_date']=strtotime($psdate);
						
					}
				}
			
				$view->assign('list',$list);
	    	return $view->fetch();

 }
	public function waimai_log()		//日志记录
	{
		$view = new View();
		$m=new IndexModel;
		$uid=Cookie::get("user_id");
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
				
		$cookiee = Cookie::get('store_id');
		if(!empty($cookiee)){
			//获取今天所有订dan
				$firsttime = strtotime(date("Y-m-d H:i:s"));
				$lasttime = $firsttime+86400;
			if(!empty($_POST['ps_date'])){						//按日期查询
				$firsttime = strtotime($_POST['ps_date']);
				$lasttime = $firsttime+86400;
				//$lastday = strtotime(date("Y-m-d"),strtotime("$firstday +24 hours"));
			}
		//	echo date("Y-m-d H:i:s",$firsttime) ."last". date("Y-m-d H:i:s",$lasttime);
			//echo $firsttime ."last". $lasttime;
			$wh = array(
					'log_time'=>['>', $firsttime],
					'log_time'=>['<', $lasttime]
			);
			$list = Db::name("waimai_log")->where($wh)->select();
			$view->assign('list',$list);
			return $view->fetch();
		}else{
			return $view->fetch('login');
		}
	}
public function uexit()
    {
    	 $view = new View();
    	 
    	  cookie::delete('user_id');
    	 cookie::delete('user_name');
    	  cookie::delete('user_password');
    	   cookie::delete('store_id');
    	    cookie::delete('store_name');

    	return $view->fetch('login');
    	
      
    }
	public function tips(){				//新订单数量

		$count=0;
		$time1=strtotime(date("Y-m-d"))-21600;
			$where=array(
					'order_status'=>'2',
					'create_time'=>['>',$time1]
			);
			$count =Db::name('waimai_order')->where($where)->count();

		echo json_encode($count);
	}

public function printcg2($cg_number){
	$wh=array(
		'cg_number'=>$cg_number
	);
	$data=[
		'cg_status'=>1
	];
	Db::name('waimai_caigou_list')->where($wh)->update($data);
	$this->redirect('Index/newcg_list');
}




}